﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace SpLocalizer.TranslationProvider.FileFormatProviders
{
    class WordFileTranslator : IFileTranslator
    {
        public string TranslateFile(AbstarctTranslationProvider provider, string SourceXml, System.Globalization.CultureInfo FromLanguage, System.Globalization.CultureInfo ToLanguage)
        {
            const int maxlines = 10;
            const int maxchars = 10000;
            XmlDocument XmlDoc = new XmlDocument();
            XmlDoc.LoadXml(SourceXml);
            System.Collections.ArrayList buf0 = new System.Collections.ArrayList();
            System.Collections.ArrayList buf1 = new System.Collections.ArrayList();
            System.Collections.ArrayList buf2 = new System.Collections.ArrayList();
            buf0.Add(buf1);
            buf0.Add(buf2);
            int buffsize = 0;
            foreach (XmlNode Xnode in XmlDoc.SelectNodes( "//*[local-name()='t']"))
            {
               
                foreach (object obj in (buf0[0] as System.Collections.ArrayList) )
                {
                    buffsize += (obj as string).Length;
                }

                if (((buf0[0] as System.Collections.ArrayList).Count < maxlines) && ((buffsize + Xnode.InnerText.Length) < maxchars))
                {
                    (buf0[0] as System.Collections.ArrayList).Add(Xnode.InnerText);
                    (buf0[1] as System.Collections.ArrayList).Add(Xnode);

                }
                else
                {
                    string[] re = new string[(buf0[0] as System.Collections.ArrayList).Count];
                    for (int i=0;i<(buf0[0] as System.Collections.ArrayList).Count;i++)
                    {
                        re[i] = Convert.ToString((buf0[0] as System.Collections.ArrayList)[i]);
                    }
                   string[] result  =  provider.TranslateArray(re,FromLanguage,ToLanguage);
                   for (int i = 0; i < (buf0[0] as System.Collections.ArrayList).Count; i++)
                   {
                       System.Collections.ArrayList bufF = (buf0[1] as System.Collections.ArrayList) ;
                       (bufF[i] as XmlNode).InnerText = result[i];
                   } 
                   buffsize = 0;
                   buf0[0] = new System.Collections.ArrayList();
                   buf0[1] = new System.Collections.ArrayList();
                }

                string[] re1 = new string[(buf0[0] as System.Collections.ArrayList).Count];
                for (int i = 0; i < (buf0[0] as System.Collections.ArrayList).Count; i++)
                {
                    re1[i] = Convert.ToString((buf0[0] as System.Collections.ArrayList)[i]);
                }
                string[] result1 = provider.TranslateArray(re1, FromLanguage, ToLanguage);
                for (int i = 0; i < (buf0[0] as System.Collections.ArrayList).Count; i++)
                {
                    System.Collections.ArrayList bufF = (buf0[1] as System.Collections.ArrayList);
                    (bufF[i] as XmlNode).InnerText = result1[i];
                }
                buffsize = 0;
                buf0[0] = new System.Collections.ArrayList();
                buf0[1] = new System.Collections.ArrayList();

            }
   
            return XmlDoc.InnerXml;
        }
    }
}
